package com.example.springaop.aop.botechotext;

import com.dingtalk.open.app.api.callback.OpenDingTalkCallbackListener;
import com.dingtalk.open.app.api.models.bot.ChatbotMessage;
import com.example.springaop.aop.util.BotReplierRfx;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collections;

/**
 * 常见问题
 * https://opensource.dingtalk.com/developerpedia/docs/learn/stream/faq
 *
 *
 * 6 Stream 模式下，是连的钉钉的哪个域名？
 * 补充问题：部署的服务器外网访问受限，需要按域名 开通外网访问权限
 *
 * 目前使用了两个域名
 *
 * api.dingtalk.com
 * wss-open-connection.dingtalk.com
 *
 *
 */
@Component
public class BotEchoTextConsumer implements OpenDingTalkCallbackListener<ChatbotMessage, Void> {

    @Override
    public Void execute(ChatbotMessage message) {
        String response = String.format("echo received message: [%s]", message.getText().getContent().trim());
        try {
            BotReplierRfx.fromWebhook(message.getSessionWebhook()).replyText(response+"清理中请稍后", Collections.singletonList(message.getSenderStaffId()));
            //业务操作
//            Thread.sleep(3000);

//            BotReplierRfx.fromWebhook(message.getSessionWebhook()).replyMarkdown(response ,"清理完成", Collections.singletonList(message.getSenderStaffId()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}